package com.mtsmda.test.comanda.d;

public class SpiralArray {

	public static void main(String[] args) {

		int spiralAr[][] = new int[9][9];
		fillArrayRandomValues(spiralAr);
		showArray(spiralAr);
		System.out.println("Spiral:");
		showArraySpiral(spiralAr);

	}

	public static void fillArrayRandomValues(int ar[][]) {
		for (int i = 0; i < ar.length; i++) {
			for (int j = 0; j < ar[i].length; j++) {
				ar[i][j] = (int) (Math.random() * 100);
			}
		}
	}

	public static StringBuilder showArray(int ar[][]) {
		StringBuilder builder = new StringBuilder(DirectoryTree.ENTER
				+ "Simple show array: " + DirectoryTree.ENTER);
		for (int i = 0; i < ar.length; i++) {
			for (int j = 0; j < ar[i].length; j++) {
				System.out.print(ar[i][j] + DirectoryTree.TAB);
				builder.append(ar[i][j]).append(DirectoryTree.TAB);
			}
			System.out.println();
			builder.append(DirectoryTree.ENTER);
		}
		return builder;
	}

	public static StringBuilder showArraySpiral(int ar[][]) {
		StringBuilder builder = new StringBuilder(DirectoryTree.ENTER
				+ "Spiral show array: " + DirectoryTree.ENTER);
		for (int i = ar.length - 1, j = 0; i > 0; i--, j++) {
			for (int k = j; k < i; k++) {
				System.out.print(ar[j][k] + DirectoryTree.TAB);
				builder.append(ar[j][k]).append(DirectoryTree.TAB);
			}
			for (int k = j; k < i; k++) {
				System.out.print(ar[k][i] + DirectoryTree.TAB);
				builder.append(ar[k][i]).append(DirectoryTree.TAB);
			}
			for (int k = i; k > j; k--) {
				System.out.print(ar[i][k] + DirectoryTree.TAB);
				builder.append(ar[i][k]).append(DirectoryTree.TAB);
			}
			for (int k = i; k > j; k--) {
				System.out.print(ar[k][j] + DirectoryTree.TAB);
				builder.append(ar[k][j]).append(DirectoryTree.TAB);
			}
		}
		return builder;
	}

}